package graphics;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * Provides the {@link BufferedImage} objects for drawing the maze, by loading them into static variables available to every instance of this class.
 * @author Nils Klement
 *
 */
public class TileSprite {
	private static String imageFilePath = "resources/dungeon_tiles/";
	
	//normally unused blank field
	private static BufferedImage BLANK;
	
	//	---folder "objects":-----
	
	private static BufferedImage LADDER_BRIGHT;
	private static BufferedImage LADDER_DARK;
	
	private static BufferedImage EXIT_ARC_EAST_BRIGHT;
	private static BufferedImage EXIT_ARC_EAST_DARK;
	
	private static BufferedImage EXIT_ARC_WEST_BRIGHT;
	private static BufferedImage EXIT_ARC_WEST_DARK;
	
	//	-------------------------

	//	---folder "junctions":---
	private static BufferedImage CROSS_BRIGHT;
	private static BufferedImage CROSS_DARK;
	
	private static BufferedImage LRD_BRIGHT;
	private static BufferedImage LRD_DARK;	

	private static BufferedImage LUD_BRIGHT;
	private static BufferedImage LUD_DARK;	

	private static BufferedImage LRU_BRIGHT;
	private static BufferedImage LRU_DARK;	

	private static BufferedImage RUD_BRIGHT;
	private static BufferedImage RUD_DARK;
	//	-------------------------
	

	//	---folder "corners":-----
	private static BufferedImage LD_BRIGHT;
	private static BufferedImage LD_DARK;
	
	private static BufferedImage LU_BRIGHT;
	private static BufferedImage LU_DARK;	

	private static BufferedImage RD_BRIGHT;
	private static BufferedImage RD_DARK;	

	private static BufferedImage RU_BRIGHT;
	private static BufferedImage RU_DARK;
	//	-------------------------
	

	//	---folder "straight":----
	private static BufferedImage UD_BRIGHT;
	private static BufferedImage UD_DARK;	

	private static BufferedImage LR_BRIGHT;
	private static BufferedImage LR_DARK;
	//	-------------------------
	
	
	//	---folder "deadends":----
	private static BufferedImage U_BRIGHT;
	private static BufferedImage U_DARK;

	private static BufferedImage D_BRIGHT;
	private static BufferedImage D_DARK;

	private static BufferedImage L_BRIGHT;
	private static BufferedImage L_DARK;

	private static BufferedImage R_BRIGHT;
	private static BufferedImage R_DARK;
	//	-------------------------
	
	//attempts to load the image files for the maze
	static{
		try {
			BLANK = ImageIO.read(new File(imageFilePath+"blank.png"));
			
			//	--------------------------------------------	objects		--------------------------------------------
			
			LADDER_BRIGHT = ImageIO.read(new File(imageFilePath+"objects/ladder_bright.png"));
			LADDER_DARK = ImageIO.read(new File(imageFilePath+"objects/ladder_dark.png"));
			
			EXIT_ARC_WEST_BRIGHT = ImageIO.read(new File(imageFilePath+"objects/exit_arc_west_bright.png"));
			EXIT_ARC_WEST_DARK = ImageIO.read(new File(imageFilePath+"objects/exit_arc_west_dark.png"));
			
			EXIT_ARC_EAST_BRIGHT = ImageIO.read(new File(imageFilePath+"objects/exit_arc_east_bright.png"));
			EXIT_ARC_EAST_DARK = ImageIO.read(new File(imageFilePath+"objects/exit_arc_east_dark.png"));
			
			
			//	--------------------------------------------	junctions	--------------------------------------------	
			CROSS_BRIGHT = ImageIO.read(new File(imageFilePath+"junctions/cross_junction_bright.png"));
			CROSS_DARK = ImageIO.read(new File(imageFilePath+"junctions/cross_junction_dark.png"));
												
			LRD_BRIGHT = ImageIO.read(new File(imageFilePath+"junctions/LRD_junction_bright.png"));
			LRD_DARK = ImageIO.read(new File(imageFilePath+"junctions/LRD_junction_dark.png"));
			
			LUD_BRIGHT = ImageIO.read(new File(imageFilePath+"junctions/LUD_junction_bright.png"));
			LUD_DARK = ImageIO.read(new File(imageFilePath+"junctions/LUD_junction_dark.png"));

			LRU_BRIGHT = ImageIO.read(new File(imageFilePath+"junctions/LRU_junction_bright.png"));
			LRU_DARK = ImageIO.read(new File(imageFilePath+"junctions/LRU_junction_dark.png"));	

			RUD_BRIGHT = ImageIO.read(new File(imageFilePath+"junctions/RUD_junction_bright.png"));
			RUD_DARK = ImageIO.read(new File(imageFilePath+"junctions/RUD_junction_dark.png"));
						
			//	--------------------------------------------	corners		--------------------------------------------
			LD_BRIGHT = ImageIO.read(new File(imageFilePath+"corners/LD_corner_bright.png"));
			LD_DARK = ImageIO.read(new File(imageFilePath+"corners/LD_corner_dark.png"));	

			LU_BRIGHT = ImageIO.read(new File(imageFilePath+"corners/LU_corner_bright.png"));
			LU_DARK = ImageIO.read(new File(imageFilePath+"corners/LU_corner_dark.png"));
			
			RD_BRIGHT = ImageIO.read(new File(imageFilePath+"corners/RD_corner_bright.png"));
			RD_DARK = ImageIO.read(new File(imageFilePath+"corners/RD_corner_dark.png"));	

			RU_BRIGHT = ImageIO.read(new File(imageFilePath+"corners/RU_corner_bright.png"));
			RU_DARK = ImageIO.read(new File(imageFilePath+"corners/RU_corner_dark.png"));
			
			//	--------------------------------------------	deadends	--------------------------------------------
			U_BRIGHT = ImageIO.read(new File(imageFilePath+"deadends/U_deadend_bright.png"));
			U_DARK = ImageIO.read(new File(imageFilePath+"deadends/U_deadend_dark.png"));
			
			D_BRIGHT = ImageIO.read(new File(imageFilePath+"deadends/D_deadend_bright.png"));
			D_DARK = ImageIO.read(new File(imageFilePath+"deadends/D_deadend_dark.png"));
			
			R_BRIGHT = ImageIO.read(new File(imageFilePath+"deadends/R_deadend_bright.png"));
			R_DARK = ImageIO.read(new File(imageFilePath+"deadends/R_deadend_dark.png"));
			
			L_BRIGHT = ImageIO.read(new File(imageFilePath+"deadends/L_deadend_bright.png"));
			L_DARK = ImageIO.read(new File(imageFilePath+"deadends/L_deadend_dark.png"));
			
			//	--------------------------------------------	straight	--------------------------------------------
			UD_BRIGHT = ImageIO.read(new File(imageFilePath+"straight/UD_straight_bright.png"));
			UD_DARK = ImageIO.read(new File(imageFilePath+"straight/UD_straight_dark.png"));
			
			LR_BRIGHT = ImageIO.read(new File(imageFilePath+"straight/LR_straight_bright.png"));
			LR_DARK = ImageIO.read(new File(imageFilePath+"straight/LR_straight_dark.png"));
			
		}
		catch (IOException e) {
			throw new IllegalArgumentException(e + " -> Error loading images.");
		}
	}
	
	private final TileShape shape;
	
	TileSprite(TileShape inputShape){
		shape = inputShape;
	}
	//loads the specific image corresponding to the tile's shape
	private BufferedImage loadImage(boolean illuminated){
		switch (shape) {
		case CROSS:
			if(illuminated){
				return CROSS_BRIGHT;
			}
			else{
				return CROSS_DARK;
			}			
		case LRD:
			if(illuminated){
				return LRD_BRIGHT;
			}
			else{
				return LRD_DARK;
			}
		case LUD:
			if(illuminated){
				return LUD_BRIGHT;
			}
			else{
				return LUD_DARK;
			}
		case LRU:
			if(illuminated){
				return LRU_BRIGHT;
			}
			else{
				return LRU_DARK;
			}
		case RUD:
			if(illuminated){
				return RUD_BRIGHT;
			}
			else{
				return RUD_DARK;
			}
		case LD:
			if(illuminated){
				return LD_BRIGHT;
			}
			else{
				return LD_DARK;
			}
		case LU:
			if(illuminated){
				return LU_BRIGHT;
			}
			else{
				return LU_DARK;
			}
		case RD:
			if(illuminated){
				return RD_BRIGHT;
			}
			else{
				return RD_DARK;
			}
		case RU:
			if(illuminated){
				return RU_BRIGHT;
			}
			else{
				return RU_DARK;
			}
		case UD:
			if(illuminated){
				return UD_BRIGHT;
			}
			else{
				return UD_DARK;
			}
		case LR:
			if(illuminated){
				return LR_BRIGHT;
			}
			else{
				return LR_DARK;
			}
		case U:
			if(illuminated){
				return U_BRIGHT;
			}
			else{
				return U_DARK;
			}
		case D:
			if(illuminated){
				return D_BRIGHT;
			}
			else{
				return D_DARK;
			}
		case L:
			if(illuminated){
				return L_BRIGHT;
			}
			else{
				return L_DARK;
			}
		case R:
			if(illuminated){
				return R_BRIGHT;
			}
			else{
				return R_DARK;
			}
		default:
			throw new IllegalArgumentException("TileShape not set!");
		}
	}
	
	//returns the bright version of this tile
	BufferedImage getLight(){
		BufferedImage image = loadImage(true);
		return image;
	}
	//returns the dark version of this tile
	BufferedImage getDark(){
		BufferedImage image = loadImage(false);
		return image;
	}
	
	public static BufferedImage loadObject(TileObject object, boolean illuminated){
		switch (object) {
		case BLANK:
			return BLANK;
		case LADDER:
			if(illuminated){
				return LADDER_BRIGHT;
			}
			return LADDER_DARK;
		case EXIT_EAST:
			if(illuminated){
				return EXIT_ARC_EAST_BRIGHT;
			}
			return EXIT_ARC_EAST_DARK;
		case EXIT_WEST:
			if(illuminated){
				return EXIT_ARC_WEST_BRIGHT;
			}
			return EXIT_ARC_WEST_DARK;
		default:
			return null;
		}
	}
	
}
